// pages/ucenter/add_address/add_address.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
      index:-1,
      addressList:[],
      address:{
        name:'',
        phone:'',
        postnum:'',
        region:[],
        region_input:'',
        add:''
      }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      let shareid = options.shareid;

      if (shareid){
          wx.setStorageSync('shareid', shareid);
      }
      
      let addresslist = wx.getStorageSync('addressList');
      let index = options.index;
      let address = this.data.address;
      let reg = /.+?(省|市|自治区|自治州|县|区)/g;

      if (index){
        address.name = addresslist[index].name;
        address.phone = addresslist[index].phone;
        address.postnum = addresslist[index].postnum;
        address.add = addresslist[index].address;
        address.region = addresslist[index].region;
        address.region_input = addresslist[index].region_input;
        this.setData({
          address:address,
          index:index
        })
      }

      
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
      
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let addresslist = wx.getStorageSync('addressList');

        if (!addresslist){
          addresslist = [];
        }
        this.setData({
          addressList:addresslist
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
      return {
        title:'sharetest',
        path:'/pages/index/index?shareid=' + '54185418'
      } 
    },
    /**
     *(定位事件)
     * @param {*} e 
     * @徐志鹏
     * @2021-2-10 
     */
    region:function(e){
      console.log(e);
      let that = this;
      let region = {};
      let _address = that.data.address;
      wx.getLocation({
        type:'gcj02',
        isHighAccuracy:true,
        altitude:'true',
        success(res){
          region.latitude = res.latitude;
          region.longitude = res.longitude;
        }
      });
      wx.chooseLocation({
        latitude:region.latitude,
        longitude:region.longitude,
        success(res){
          _address.add = res.address;
          that.setData({
            address:_address
          })
        }
      });
      
    },
    /**
     *(picker组件确定按钮监听事件)
     * @param {*} e 
     * @徐志鹏
     * @2021-2-10 
     */
    bindchange:function(e){
      let address = this.data.address;
      address.region = e.detail.value;
      address.region_input = e.detail.value.join(' ');
      address.postnum = e.detail.postcode;
      this.setData({
        address:address
      })
    },
    /**
     *(input(名字)监听事件)
     * @param {*} e 
     * @徐志鹏
     * @2021-2-10 
     */
    bindinput_name:function(e){
      let _address = this.data.address;

      _address.name = e.detail.value;
      this.setData({
        address:_address
      })
    },
    /**
     *(input(电话)监听事件)
     * @param {*} e 
     * @徐志鹏
     * @2021-2-10 
     */
    bindinput_phone:function(e){
      let _address = this.data.address;

      _address.phone = e.detail.value;
      this.setData({
        address:_address
      })
    },
    /**
     *(input(地址)监听事件)
     * @param {*} e 
     * @徐志鹏
     * @2021-2-10 
     */
    bindinput_add:function(e){
      let _address = this.data.address;

      _address.add = e.detail.value;
      this.setData({
        address:_address
      })
    },
    /**
     *(input(邮编)监听事件)
     * @param {*} e 
     * @徐志鹏
     * @2021-2-10 
     */
    bindinput_postcode:function(e){
      let _address = this.data.address;

      _address.postnum = e.detail.value;
      this.setData({
        address:_address
      })
    },
    /**
     *(确定按钮点击事件)
     * @param {*} e 
     * @徐志鹏
     * @2021-2-10 
     */
    submit:function(){
      let index = this.data.index;
      let _address = this.data.address;
      let addresslist = this.data.addressList;
      if (_address.name.length === 0 || _address.phone.length === 0 || _address.region_input.length === 0 || _address.add.length === 0 || _address.postnum.length === 0){
        wx.showToast({
          title: '请填写完整信息哦！',
          icon:'none'
        });
        return;
      }
      if (_address.phone.length < 11){
        wx.showToast({
          title:'请填写11位手机号哦！',
          icon:'none'
        });
        return;
      }
      if (index === -1){
        let address = {};
        address.id = addresslist.length;
        address.name = _address.name;
        address.phone = _address.phone;
        address.address = _address.add;
        address.postnum = _address.postnum;
        address.region = _address.region;
        address.region_input = _address.region_input;
        addresslist.push(address);
      } else {
        addresslist[index].name = _address.name;
        addresslist[index].phone = _address.phone;
        addresslist[index].address = _address.add;
        addresslist[index].postnum = _address.postnum;
        addresslist[index].region_input = _address.region_input;
        addresslist[index].region = _address.region;
      }
      this.setData({
        addressList:addresslist
      })
      wx.setStorageSync('addressList', addresslist);
      wx.navigateBack({
        delta: 1,
      })
      
    },
    /**
     * @date 2021-3-15
     * @author 徐志鹏
     * @param {*} e 
     */
    chooseContact:function(e){
      let that = this;
      let address = that.data.address;
      wx.chooseContact({
        success:function(res){
          address.name = res.displayName;
          address.phone = res.phoneNumber;
          that.setData({
            address:address
          })
        },
        fail:function(){

        }
      })
    }
})
